﻿using System;
using System.IO;
using System.Data;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;

using PriErp.Tools.Extensions;

namespace PriErp.Commands
{
    public partial class BrowserTextBox : UserControl
    {
        public BrowserTextBox()
        {
            InitializeComponent();
            this.txtPath.LostFocus += new EventHandler(PathChanged);
        }

        /// <summary>
        /// 文件信息变更事件
        /// </summary>
        [Description("文件信息变更事件")]
        public event EventHandler OnPathChanged;

        /// <summary>
        /// 文件信息变更
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void PathChanged(object sender, EventArgs e)
        {
            bool v = ValidatePath();
            if (!v)
                txtPath.Text = string.Empty;
            else if (OnPathChanged != null)
                OnPathChanged(sender, e);
        }

        /// <summary>
        /// 获取、设置水印文字
        /// </summary>
        [Browsable(true)]
        [Category("Appearance")]
        [Description("获取、设置水印文字")]
        public string MarketText
        {
            get
            {
                return this.txtPath.WatermarkText;
            }
            set
            {
                this.txtPath.WatermarkText = value;
            }
        }

        /// <summary>
        /// 获取、设置水印字体
        /// </summary>
        [Browsable(true)]
        [Category("Appearance")]
        [Description("获取、设置水印字体")]
        public Font MarketFont
        {
            get
            {
                return this.txtPath.WatermarkFont;
            }
            set
            {
                this.txtPath.WatermarkFont = value;
            }
        }

        /// <summary>
        /// 获取、设置水印文字颜色
        /// </summary>
        [Browsable(true)]
        [Category("Appearance")]
        [Description("获取、设置水印文字颜色")]
        public Color MarketColor
        {
            get
            {
                return this.txtPath.WatermarkColor;
            }
            set
            {
                this.txtPath.WatermarkColor = value;
            }
        }

        /// <summary>
        /// 获取、设置输入框文本样色
        /// </summary>
        [Browsable(true)]
        [Category("Appearance")]
        [Description("获取、设置输入框文本样色")]
        public Color PathColor
        {
            get
            {
                return this.txtPath.ForeColor;
            }
            set
            {
                this.txtPath.ForeColor = value;
            }
        }

        /// <summary>
        /// 获取、设置输入框文本字体
        /// </summary>
        [Browsable(true)]
        [Category("Appearance")]
        [Description("获取、设置输入框文本字体")]
        public Font PathFontSize
        {
            get
            {
                return this.txtPath.Font;
            }
            set
            {
                this.txtPath.Font = value;
            }
        }

        /// <summary>
        /// 获取、设置输入框高度
        /// </summary>
        [Browsable(true)]
        [Category("Appearance")]
        [Description("获取、设置输入框高度")]
        public int PathHeight
        {
            get
            {
                return this.txtPath.Height;
            }
            set
            {
                this.txtPath.Height = value;
            }
        }

        /// <summary>
        /// 获取、设置输入框文字
        /// </summary>
        [Browsable(true)]
        [Category("Appearance")]
        [Description("获取、设置输入框文字")]
        public string PathText
        {
            get
            {
                return this.txtPath.Text;
            }
            set
            {
                this.txtPath.Text = value;
                PathChanged(this, EventArgs.Empty);
            }
        }

        /// <summary>
        /// 获取、设置过滤文件类型
        /// </summary>
        [Browsable(true)]
        [Category("Appearance")]
        [Description("获取、设置过滤文件类型")]
        public string Fliter
        {
            get
            {
                return openDialog.Filter;
            }
            set
            {
                openDialog.Filter = value;
            }
        }

        private void txtPath_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop, false))
                e.Effect = DragDropEffects.All;
        }

        private void txtPath_DragDrop(object sender, DragEventArgs e)
        {
            string[] files = e.Data.GetData(DataFormats.FileDrop).AsType<string[]>(null);
            if (files == null || files.Length < 1)
                return;
            PathText = files[0];
        }

        private void btnPath_Click(object sender, EventArgs e)
        {
            DialogResult result = openDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                txtPath.Text = openDialog.FileName;
            }
        }

        private void txtPath_Leave(object sender, EventArgs e)
        {
            bool v = ValidatePath();
            if (!v)
                txtPath.Text = string.Empty;
        }

        /// <summary>
        /// 验证路径是否符合条件
        /// </summary>
        /// <returns></returns>
        private bool ValidatePath()
        {
            string path = txtPath.Text;
            bool v = File.Exists(path);
            if (v && !string.IsNullOrEmpty(Fliter))
            {
                string[] _fliter = Fliter.Split('|')[1].Split(';');
                int iLen = _fliter.Length;
                for (int i = 0; i < iLen; i++)
                {
                    string tmp = _fliter[i];
                    if (tmp == "*.*")
                        return true;
                    _fliter[i] = tmp.Substring(1);
                }
                string exten = Path.GetExtension(path).ToLower();
                for (int i = 0; i < iLen; i++)
                {
                    string tmp = _fliter[i].ToLower();
                    if (exten == tmp)
                        return true;
                }
                v = !v;
            }
            return v;
        }
    }
}
